package com.dhh.sky.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.dhh.sky.R;
import com.dhh.sky.SDAApplication;
import com.dhh.sky.util.JsonKeys;
import com.microsoft.live.LiveConnectClient;
import com.microsoft.live.LiveOperation;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class BatchUploadService extends IntentService {
	private LiveConnectClient mClient;
	
	public BatchUploadService() {
		super("BatchUploadService");
	}

	@Override
	public void onHandleIntent(Intent intent) {
		String[] names = intent.getExtras().getStringArray("names");
		String[] files = intent.getExtras().getStringArray("files");
		String path = intent.getExtras().getString("path");
		mClient = ((SDAApplication) getApplication()).getConnectClient();
		
		if(names!=null && names.length>0){
			doUplaod(names,files,path);
		}
	}
	
	private void doUplaod(String[] names,String[] files,String path){
		List<String> nlst = new ArrayList<String>();
		List<String> flst = new ArrayList<String>();
		List<String> mlst = new ArrayList<String>();
		
		Notification notification = new Notification(R.drawable.ic_launcher, "Start uploading ", System.currentTimeMillis());
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
        notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_up_s);    
        notification.contentView.setTextViewText(R.id.status_text, "Initialize to upload files...");
        notification.contentView.setProgressBar(R.id.status_progress, files.length, 0, false);
        Intent it = new Intent(this,FileListActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, it, 0);
        notification.contentIntent = pendingIntent;
		NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.notify(42, notification);
		
		for(int i=0; i<names.length; i++){
			String name = names[i];
			String file = files[i];
			notification.contentView.setProgressBar(R.id.status_progress, files.length, i, false);
			notification.contentView.setTextViewText(R.id.status_text, "Uploading "+name);
			notificationManager.notify(42, notification);
			String msg = upload(name,file,path);
			
			if(msg!=null){
				nlst.add(name);
				flst.add(file);
				mlst.add(msg);
			}
		}
		
		notification.contentView.setProgressBar(R.id.status_progress, files.length, files.length, false);
		notification.contentView.setTextViewText(R.id.status_text, "Upload files successfully.");
		notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_yes);	
		if(!mlst.isEmpty()){
			notification.contentView.setTextViewText(R.id.status_text, "Issues while uploading!");
			notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_warning);	
		}
		notificationManager.notify(42, notification);		
	}
	
	private String upload(String name,String file,String path){
		File uploadFile = new File(file);
		String msg = null;
		if(uploadFile.exists()){
			try {
				LiveOperation operation = mClient.upload(path, name, uploadFile);
				JSONObject result = operation.getResult();
				if(result.has(JsonKeys.ERROR)){
					msg = result.getString(JsonKeys.ERROR);
				}
			} catch (Exception e) {
				e.printStackTrace();
				msg = e.getMessage();
			}
		}else{
			msg = "The file does not exist!";
		}
		
		return msg;
		
	}
}
